package com.wechat.page.login;

import com.swak.fx.support.AbstractComp;
import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.ui.UIButton;
import com.swak.fx.support.ui.UIRadioButton;
import com.swak.fx.support.ui.UITextField;
import com.swak.utils.StringUtils;
import com.wechat.Cons;
import com.wechat.enums.Models;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextFlow;

/**
 * 
 * 模式切换
 * 
 * @author 618lf
 */
@FXMLView(title = "模式切换", value = "ModelSwitchComp.fxml", css = "ModelSwitchComp.css", stageStyle = "TRANSPARENT")
public class ModelSwitchComp extends AbstractComp {

	@FXML
	private UIRadioButton switchBtnP2p;
	@FXML
	private UIRadioButton switchBtnSer;
	@FXML
	private Label modelAddrLabel;
	@FXML
	private UITextField modelAddrInput;
	@FXML
	private TextFlow modelDescribe;
	@FXML
	private UIButton confirmBtn;

	private Text p2pText = new Text("P2P局域网模式，通过不同的服务口令，可以建立私域聊天；如果使用默认服务口令，可以和局域网内的所有人聊天；");
	private Text serText = new Text("服务集群模式，即客户端服务器模式，需要输入服务器地址，类似Ip:Port；有多个服务器用‘,’隔开；");

	public ModelSwitchComp(AbstractPage page) {
		super(page);
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		this.confirmBtn.Success().setText("确定");
		this.modelDescribe.setLineSpacing(10);
		this.modelDescribe.setTextAlignment(TextAlignment.JUSTIFY);
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
		this.switchBtnP2p.addEventFilter(MouseEvent.ANY, e -> {
			e.consume();
			if (e.getEventType() == MouseEvent.MOUSE_CLICKED) {
				this.modelProperty().set(Models.P2P);
			}
		});
		this.switchBtnSer.addEventFilter(MouseEvent.ANY, e -> {
			e.consume();
			if (e.getEventType() == MouseEvent.MOUSE_CLICKED) {
				this.modelProperty().set(Models.SerGroup);
			}
		});
		this.confirmBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			this.settings.storeConfig();
			this.$emitInPage("page:login");
			// this.$toast(ToastConfig.of().setMessage("错误2！").setTheme(Themes.Success)).show();
		});
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
		this.modelProperty().addListener((ob, o, n) -> {
			this.$runOnUI(() -> {
				this.switchBtnP2p.setSelected(false);
				this.switchBtnSer.setSelected(false);
				if (n == null || n == Models.P2P) {
					this.switchBtnP2p.setSelected(true);
					this.modelAddrLabel.setText("服务口令：");
					this.modelDescribe.getChildren().clear();
					this.modelDescribe.getChildren().add(p2pText);
					this.modelAddrInput.setPromptText("10000~60000之间的数字");
				} else {
					this.switchBtnSer.setSelected(true);
					this.modelAddrLabel.setText("服务地址：");
					this.modelDescribe.getChildren().clear();
					this.modelDescribe.getChildren().add(serText);
					this.modelAddrInput.setPromptText("输入IP:PORT,多个用‘,’隔开");
				}
				this.settings.putConfig(Cons.Network_Model_Key, n != null ? n.name() : StringUtils.EMPTY);
			});
		});
		this.modelAddrInput.textProperty().addListener((ob, o, n) -> {
			this.settings.putConfig(Cons.Network_Sers_Key, n);
		});
	}

	@Override
	public void initializeFinish() {
		super.initializeFinish();

		// 模式
		String modelStr = this.settings.getConfig(Cons.Network_Model_Key);
		Models model = StringUtils.isBlank(modelStr) ? Models.P2P : Models.valueOf(modelStr);
		this.modelProperty().set(model);

		// 服务地址
		String modelSer = this.settings.getConfig(Cons.Network_Sers_Key);
		this.modelAddrInput.setText(modelSer);
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private ObjectProperty<Models> modelProperty = new SimpleObjectProperty<>();

	public ObjectProperty<Models> modelProperty() {
		return modelProperty;
	}
}
